﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.Brand;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Ekton_Manage_Business
{
    public class BrandBusiness : IBrandBusiness
    {
        private readonly SerializeParameter _serialize;
        private readonly IBaseRepository<brand_msg, int> _baseBrandMsgRepository;
        private readonly IBaseRepository<series_msg, int> _baseSeriesMsgRepository;
        private readonly IBaseRepository<platform_msg, int> _basePlatformMsgRepository;

        public BrandBusiness(SerializeParameter serialize, IBaseRepository<brand_msg, int> baseBrandMsgRepository, IBaseRepository<series_msg, int> baseSeriesMsgRepository, IBaseRepository<platform_msg, int> basePlatformMsgRepository)
        {
            _serialize = serialize;
            _baseBrandMsgRepository = baseBrandMsgRepository;
            _baseSeriesMsgRepository = baseSeriesMsgRepository;
            _basePlatformMsgRepository = basePlatformMsgRepository;
        }

        /// <summary>
        ///  查询全部品牌信息
        /// </summary>
        /// 
        public async Task<Tuple<List<brand_msg>, int>> GetBrandList(QueryDataParame query)
        {
            var brandInfo = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<brand_msg, bool>> expression = m => true;
            if (brandInfo.ContainsKey("product_brand") && !string.IsNullOrEmpty(brandInfo["product_brand"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.product_brand.Contains(brandInfo["product_brand"].ToString()));
            }
            return await _baseBrandMsgRepository.QueryPageData(query, expression);
        }

        /// <summary>
        ///  编辑品牌信息
        /// </summary>
        /// 
        public async Task<Message> PutBrandInfo(DtoBrandInfo request)
        {
            var brandInfo = await _baseBrandMsgRepository.QuerySingleData(request.id);
            if (brandInfo == null) return new Message(false, 202);
            brandInfo.product_brand = request.product_brand;
            brandInfo.yield = request.yield;
            brandInfo.remark = request.remark;
            var result = await _baseBrandMsgRepository.UpdateSingleData(brandInfo);
            return new Message(result.Item1);
        }
        /// <summary>
        ///  添加品牌信息
        /// </summary>
        /// 
        public async Task<Message> PostBrandInfo(DtoBrandInfo request)
        {
            var list = await _baseBrandMsgRepository.InsertSingleData(new brand_msg
            {
                product_brand = request.product_brand,
                yield = request.yield,
                remark = request.remark
            });
            return new Message(list.Item1);
        }

        /// <summary>
        ///  删除品牌信息
        /// </summary>
        /// 
        public async Task<Message> DeleteBrandInfo(int id)
        {
            var list = await _baseBrandMsgRepository.DeleteSingleData(id);
            return new Message(list);
        }

        /// <summary>
        ///  查询全部系列信息
        /// </summary>
        /// 
        public async Task<Tuple<List<series_msg>, int>> GetSequenceInfo(QueryDataParame query)
        {
            var brandInfo = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<series_msg, bool>> expression = m => true;
            if (brandInfo.ContainsKey("seriesname") && !string.IsNullOrEmpty(brandInfo["seriesname"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.seriesname.Contains(brandInfo["seriesname"].ToString()));
            }
            if (brandInfo.ContainsKey("farentlevel") && !string.IsNullOrEmpty(brandInfo["farentlevel"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.farentlevel.Contains(brandInfo["farentlevel"].ToString()));
            }
            return await _baseSeriesMsgRepository.QueryPageData(query, expression);
        }

        /// <summary>
        ///  编辑系列信息
        /// </summary>
        /// 
        public async Task<Message> PutSequenceInfo(DtoSequenceInfo request)
        {
            var sequenceInfo = await _baseSeriesMsgRepository.QuerySingleData(request.id);
            if (sequenceInfo == null) return new Message(false, 202);
            sequenceInfo.seriesname = request.seriesname;
            sequenceInfo.farentlevel = request.farentlevel;
            sequenceInfo.remark = request.remark;
            var result = await _baseSeriesMsgRepository.UpdateSingleData(sequenceInfo);
            return new Message(result.Item1);
        }
        /// <summary>
        ///  添加系列信息
        /// </summary>
        /// 
        public async Task<Message> PostSequenceInfo(DtoSequenceInfo request)
        {
            var list = await _baseSeriesMsgRepository.InsertSingleData(new series_msg
            {
                seriesname = request.seriesname,
                farentlevel = request.farentlevel,
                brand_id = request.brand_id,
                remark = request.remark,
                platform_id = request.platform_id
            });
            return new Message(list.Item1);
        }
        /// <summary>
        ///  删除序列信息
        /// </summary>
        /// 
        public async Task<Message> DeleteSequenceInfo(int id)
        {
            var list = await _baseSeriesMsgRepository.DeleteSingleData(id);
            return new Message(list);
        }
        /// <summary>
        ///  查询全部平台信息
        /// </summary>
        /// 
        public async Task<Tuple<List<platform_msg>, int>> GetPlatformInfo(QueryDataParame query)
        {
            var brandInfo = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<platform_msg, bool>> expression = m => true;
            if (brandInfo.ContainsKey("platform_name") && !string.IsNullOrEmpty(brandInfo["platform_name"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.platform_name.Contains(brandInfo["platform_name"].ToString()));
            }
            if (brandInfo.ContainsKey("father") && !string.IsNullOrEmpty(brandInfo["father"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.father.Contains(brandInfo["father"].ToString()));
            }
            return await _basePlatformMsgRepository.QueryPageData(query, expression);
        }
        /// <summary>
        ///  编辑平台信息
        /// </summary>
        /// 
        public async Task<Message> PutPlatformInfo(DtoPlatformInfo request)
        {
            var platformInfo = await _basePlatformMsgRepository.QuerySingleData(request.id);
            //根据传的系列id来查询他的系列名称
            var father = await _baseBrandMsgRepository.QuerySingleData(request.series_id);
            if (platformInfo == null) return new Message(false, 202);
            platformInfo.platform_name = request.platform_name;
            platformInfo.genra = request.genra;//支持牙位类型
            platformInfo.series_id = request.series_id;//关联系列id
            platformInfo.remark = request.remark;
            platformInfo.father = father.ToString();
            var result = await _basePlatformMsgRepository.UpdateSingleData(platformInfo);
            return new Message(result.Item1);
        }
        /// <summary>
        ///  添加平台信息
        /// </summary>
        /// 
        public async Task<Message> PostPlatformInfo(DtoPlatformInfo request)
        {
            var father = await _baseBrandMsgRepository.QuerySingleData(request.series_id);
            var platInfo = new platform_msg
            {
                platform_name = request.platform_name,
                genra = request.genra,
                series_id = request.series_id,
                remark = request.remark,
                father = father.ToString()
            };
            var list = await _basePlatformMsgRepository.InsertSingleData(platInfo);
            return new Message(list.Item1);
        }
        /// <summary>
        ///  删除平台信息
        /// </summary>
        /// 
        public async Task<Message> DeletePlatformInfo(int id)
        {
            var list = await _basePlatformMsgRepository.DeleteSingleData(id);
            return new Message(list);
        }
    }
}
